package com.sync.platform.controller;

import com.sync.platform.entity.SyncTask;
import com.sync.platform.entity.SyncTaskLog;
import com.sync.platform.service.SyncTaskLogService;
import com.sync.platform.service.SyncTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Task log controller for viewing synchronization logs
 */
@Controller
@RequestMapping("/task")
public class TaskLogController {

    private static final Logger logger = LoggerFactory.getLogger(TaskLogController.class);

    @Autowired
    private SyncTaskLogService syncTaskLogService;

    @Autowired
    private SyncTaskService syncTaskService;

    /**
     * View logs page
     */
    @GetMapping("/log")
    public String viewLogs(Model model) {
        // Get all tasks for the dropdown
        List<SyncTask> tasks = syncTaskService.list();
        
        // Get the most recent logs for all tasks
        List<SyncTaskLog> logs = syncTaskLogService.getRecentLogs(10);
        
        model.addAttribute("tasks", tasks);
        model.addAttribute("logs", logs);
        model.addAttribute("pageTitle", "同步平台 - 同步日志");
        
        return "task/log";
    }

    /**
     * View logs for a specific task
     */
    @GetMapping("/log/{taskId}")
    public String viewTaskLogs(@PathVariable Long taskId, Model model) {
        SyncTask task = syncTaskService.getById(taskId);
        if (task == null) {
            return "redirect:/task/log";
        }
        
        // Get logs for this specific task
        List<SyncTaskLog> logs = syncTaskLogService.getLogsByTaskId(taskId);
        
        // Get all tasks for the dropdown
        List<SyncTask> tasks = syncTaskService.list();
        
        model.addAttribute("task", task);
        model.addAttribute("tasks", tasks);
        model.addAttribute("logs", logs);
        model.addAttribute("pageTitle", "同步平台 - " + task.getName() + " 日志");
        
        return "task/log";
    }

    /**
     * Get logs for a specific task (AJAX)
     */
    @GetMapping("/log/ajax/{taskId}")
    @ResponseBody
    public Map<String, Object> getTaskLogs(@PathVariable Long taskId) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            List<SyncTaskLog> logs = syncTaskLogService.getLogsByTaskId(taskId);
            result.put("success", true);
            result.put("logs", logs);
        } catch (Exception e) {
            logger.error("Error getting logs for task {}: {}", taskId, e.getMessage(), e);
            result.put("success", false);
            result.put("message", "获取日志失败: " + e.getMessage());
        }
        
        return result;
    }
} 